﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.RDLC

Partial Class DocExlusive
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session.Item("StaffId") Is Nothing Then
            Response.Redirect("default.aspx")
        End If

        If Not Page.IsPostBack Then

            CBEBack.ConfirmText = "You are about to navigate away from this page. " & vbCrLf & "Changes will be discard!"
            CBEDownload.ConfirmText = "All the changes will be saved before you download the document!"

            lblStaffUID.Text = Session.Item("StaffId").ToString
            lblStaffType.Text = Session.Item("StaffType").ToString

            If Not Session("id") Is Nothing Then
                lblDocId.Text = Me.Session("id").ToString
                pnlEntry.Visible = True
                pnlPreview.Visible = False
                LoadEntry()
            Else
                pnlEntry.Visible = True
                pnlPreview.Visible = False
            End If
        End If
    End Sub

    Private Sub GetStaff(ByVal rep As Repeater)
        Dim TempDT As New Data.DataTable
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Try
            objDB.OpenConnection()

            objDB.dbVariable.SQLString = "SELECT StaffId,Name FROM AW_Staff WHERE StatusDelete = 0 ORDER BY Name"
            objDB.FillData(TempDT, "AW_Staff", Database.SQLCommandType.NormalString)

            objDB.CloseConnection()
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
        rep.DataSource = TempDT
        rep.DataBind()
    End Sub
    
    Protected Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Try
            objDB.OpenConnection()
            objDB.InitDbTrans()
            Dim DocExId As String
            If lblDocId.Text = String.Empty Then
                objDB.dbVariable.SQLString = "INSERT INTO AW_DocExclusive(DocExId, DocExTitle, REN, Lister, DocDate, Premise, Price, " & _
                                        "Remarks, ContactPerson, ContactNo, FaxNo, Commission, CPRemarks, CreateDate, CreateBy, UpdateDate, UpdateBy, " & _
                                        "StatusDelete) VALUES(@DocExId, @DocExTitle, @REN, @Lister, @DocDate, @Premise, @Price, " & _
                                        "@Remarks, @ContactPerson, @ContactNo, @FaxNo, @Commission, @CPRemarks, @CreateDate, " & _
                                        "@CreateBy, @UpdateDate, @UpdateBy, @StatusDelete)"
                DocExId = Guid.NewGuid.ToString
                lblUpdated.Text = "Created By : " & GetName() & " on " & Format(Now(), "dd-MMM-yyyy")
                objDB.AddParameter("UpdateDate", Data.SqlDbType.DateTime, DBNull.Value)
                objDB.AddParameter("UpdateBy", Data.SqlDbType.UniqueIdentifier, DBNull.Value)
            Else
                objDB.dbVariable.SQLString = "UPDATE AW_DocExclusive SET DocExTitle=@DocExTitle, REN=@REN, Lister=@Lister, DocDate=@DocDate, " & _
                                            "Premise=@Premise, Price=@Price, Remarks=@Remarks, ContactPerson=@ContactPerson, " & _
                                            "ContactNo=@ContactNo, FaxNo=@FaxNo, Commission=@Commission, CPRemarks=@CPRemarks, UpdateDate=@UpdateDate, " & _
                                            "UpdateBy=@UpdateBy WHERE DocExId=@DocExId"
                DocExId = lblDocId.Text
                lblUpdated.Text = "Last Updated By : " & GetName() & " on " & Format(Now(), "dd-MMM-yyyy")
                objDB.AddParameter("UpdateDate", Data.SqlDbType.DateTime, Now())
                objDB.AddParameter("UpdateBy", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.Text))
            End If
            objDB.AddParameter("DocExId", Data.SqlDbType.UniqueIdentifier, New Guid(DocExId))
            objDB.AddParameter("DocExTitle", Data.SqlDbType.NVarChar, txtTitle.Text.Trim.ToUpper)
            If txtREN.Text = String.Empty Then
                objDB.AddParameter("REN", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("REN", Data.SqlDbType.NVarChar, txtREN.Text.Trim)
            End If
            If txtLister.Text = String.Empty Then
                objDB.AddParameter("Lister", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Lister", Data.SqlDbType.NVarChar, txtLister.Text.Trim)
            End If
            If txtDate.Text = String.Empty Then
                objDB.AddParameter("DocDate", Data.SqlDbType.DateTime, DBNull.Value)
            Else
                objDB.AddParameter("DocDate", Data.SqlDbType.DateTime, DateTime.ParseExact(txtDate.Text.Trim, "dd-MMM-yyyy", Nothing))
            End If
            If txtPremise.Text = String.Empty Then
                objDB.AddParameter("Premise", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Premise", Data.SqlDbType.NVarChar, txtPremise.Text.Trim)
            End If
            If txtPrice.Text = String.Empty Then
                objDB.AddParameter("Price", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Price", Data.SqlDbType.NVarChar, txtPrice.Text.Trim)
            End If
            If txtRemarks.Text = String.Empty Then
                objDB.AddParameter("Remarks", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Remarks", Data.SqlDbType.NVarChar, txtRemarks.Text.Trim)
            End If
            If txtContactPerson.Text = String.Empty Then
                objDB.AddParameter("ContactPerson", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("ContactPerson", Data.SqlDbType.NVarChar, txtContactPerson.Text.Trim)
            End If
            If txtContact.Text = String.Empty Then
                objDB.AddParameter("ContactNo", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("ContactNo", Data.SqlDbType.NVarChar, txtContact.Text.Trim)
            End If
            If txtFax.Text = String.Empty Then
                objDB.AddParameter("FaxNo", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("FaxNo", Data.SqlDbType.NVarChar, txtFax.Text.Trim)
            End If
            If txtCommission.Text = String.Empty Then
                objDB.AddParameter("Commission", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("Commission", Data.SqlDbType.NVarChar, txtCommission.Text.Trim)
            End If
            If txtCPRemarks.Text = String.Empty Then
                objDB.AddParameter("CPRemarks", Data.SqlDbType.NVarChar, DBNull.Value)
            Else
                objDB.AddParameter("CPRemarks", Data.SqlDbType.NVarChar, txtCPRemarks.Text.Trim)
            End If
            objDB.AddParameter("CreateDate", Data.SqlDbType.DateTime, Now())
            objDB.AddParameter("CreateBy", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.Text))
            objDB.AddParameter("StatusDelete", Data.SqlDbType.TinyInt, 0)
            objDB.ExecuteQuery()
            objDB.DbTransCommit()
            objDB.CloseConnection()
            lblDocId.Text = DocExId
        Catch ex As Exception
            objDB.DbTransRollback()
            If objDB.dbVariable.objConn.State = Data.ConnectionState.Open Then
                objDB.CloseConnection()
            End If
            lblMsgError.Text = ex.Message
            divError.Visible = True
        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
    End Sub

    Private Function GetName() As String
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Dim StaffName As String = String.Empty
        Try
            objDB.OpenConnection()

            objDB.dbVariable.SQLString = "SELECT Name FROM AW_Staff WHERE StaffId=@StaffId"
            objDB.AddParameter("StaffId", Data.SqlDbType.UniqueIdentifier, New Guid(lblStaffUID.Text))
            objDB.FillData(TempDT, "AW_Staff", Database.SQLCommandType.NormalString)
            objDB.CloseConnection()
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try

        For Each dtrow As Data.DataRow In TempDT.Rows
            If dtrow.Item("Name").ToString <> "" Then
                StaffName = dtrow.Item("Name").ToString
            End If
        Next

        Return StaffName
    End Function

    Private Sub LoadEntry()
        
        Dim TempDT As New Data.DataTable
        TempDT = LoadDocExlusive()
        For Each dtrow As Data.DataRow In TempDT.Rows
            txtTitle.Text = dtrow.Item("DocExTitle").ToString
            txtREN.Text = dtrow.Item("REN").ToString
            txtDate.Text = Format(dtrow.Item("DocDate"), "dd-MMM-yyyy")
            txtLister.Text = dtrow.Item("Lister").ToString
            txtPremise.Text = dtrow.Item("Premise").ToString
            txtPrice.Text = Format(dtrow.Item("Price"), "#,##0.00")
            txtRemarks.Text = dtrow.Item("Remarks").ToString
            txtContactPerson.Text = dtrow.Item("ContactPerson").ToString
            txtContact.Text = dtrow.Item("ContactNo").ToString
            txtFax.Text = dtrow.Item("FaxNo").ToString
            txtCommission.Text = Format(dtrow.Item("Commission"), "#,##0.00")
            txtCPRemarks.Text = dtrow.Item("CPRemarks").ToString
            lblUpdated.Text = "Last Updated By : " & dtrow.Item("CreateBy").ToString & " on " & Format(dtrow.Item("CreateDate"), "dd-MMM-yyyy")
        Next
      
    End Sub

    Private Function LoadDocExlusive() As Data.DataTable
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Try
            objDB.OpenConnection()

            objDB.dbVariable.SQLString = "SELECT d.DocExTitle, d.REN, d.Lister, d.Premise, d.Price, d.Remarks, d.ContactPerson, " & _
                                        "d.ContactNo, d.Commission, d.CPRemarks, ISNULL(d.UpdateDate, d.CreateDate) as CreateDate, " & _
                                        "ISNULL(u.Initial, c.Initial) as CreateBy, d.StatusDelete, d.DocDate, d.FaxNo FROM AW_DocExclusive d " & _
                                        "LEFT JOIN AW_Staff u ON d.UpdateBy = u.StaffId LEFT JOIN AW_Staff c ON " & _
                                        "d.CreateBy = c.StaffId WHERE DocExId=@DocExId"
            objDB.AddParameter("DocExId", Data.SqlDbType.UniqueIdentifier, New Guid(lblDocId.Text))
            objDB.FillData(TempDT, "DocExclusive", Database.SQLCommandType.NormalString)
            objDB.CloseConnection()
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
        Return TempDT
    End Function

    Protected Sub btnPreview_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPreview.Click
        pnlEntry.Visible = False
        pnlPreview.Visible = True

        Dim TempDT As New Data.DataTable
        Dim CommissionDT As New Data.DataTable
        Dim GrandTotal As Decimal = 0

        'TempDT = LoadDocExlusive()
        ''edited by minginn - to allow user to preview

        DeclareColumn(TempDT)
        Dim MyRow As Data.DataRow
        MyRow = TempDT.NewRow()
        MyRow(0) = txtTitle.Text.Trim.ToUpper
        MyRow(1) = txtREN.Text.Trim
        MyRow(2) = txtLister.Text.Trim
        If txtDate.Text = String.Empty Then
            MyRow(3) = DBNull.Value
        Else
            MyRow(3) = DateTime.ParseExact(txtDate.Text.Trim, "dd-MMM-yyyy", Nothing)
        End If
        MyRow(4) = txtPremise.Text.Trim
        MyRow(5) = txtPrice.Text.Trim
        MyRow(6) = txtRemarks.Text.Trim
        MyRow(7) = txtContactPerson.Text.Trim
        MyRow(8) = txtContact.Text.Trim
        MyRow(9) = txtFax.Text.Trim
        MyRow(10) = txtCommission.Text.Trim
        MyRow(11) = txtCPRemarks.Text.Trim
        MyRow(12) = Now()
        MyRow(13) = GetName()
        TempDT.Rows.Add(MyRow)

        If TempDT IsNot Nothing Then
            'Dim objRDLC As New RDLC("report/rptDocExclusive.rdlc", "DocExclusive")
            TempDT.TableName = "dsDocExclusive_AW_DocExclusive"
            'objRDLC.AddDatabase(TempDT)
            ReportViewer1.LocalReport.DataSources.Clear()
            ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource(TempDT.TableName, TempDT))
            ReportViewer1.LocalReport.Refresh()
        End If
    End Sub

    Private Sub DeclareColumn(ByVal MyDT As Data.DataTable)
        Dim DocExTitle As Data.DataColumn = New Data.DataColumn("DocExTitle")
        DocExTitle.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(DocExTitle)
        Dim REN As Data.DataColumn = New Data.DataColumn("REN")
        REN.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(REN)
        Dim Lister As Data.DataColumn = New Data.DataColumn("Lister")
        Lister.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Lister)
        Dim DocDate As Data.DataColumn = New Data.DataColumn("DocDate")
        DocDate.DataType = System.Type.GetType("System.DateTime")
        MyDT.Columns.Add(DocDate)
        Dim Premise As Data.DataColumn = New Data.DataColumn("Premise")
        Premise.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Premise)
        Dim Price As Data.DataColumn = New Data.DataColumn("Price")
        Price.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Price)
        Dim Remarks As Data.DataColumn = New Data.DataColumn("Remarks")
        Remarks.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Remarks)
        Dim ContactPerson As Data.DataColumn = New Data.DataColumn("ContactPerson")
        ContactPerson.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(ContactPerson)
        Dim ContactNo As Data.DataColumn = New Data.DataColumn("ContactNo")
        ContactNo.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(ContactNo)
        Dim FaxNo As Data.DataColumn = New Data.DataColumn("FaxNo")
        FaxNo.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(FaxNo)
        Dim Commission As Data.DataColumn = New Data.DataColumn("Commission")
        Commission.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(Commission)
        Dim CPRemarks As Data.DataColumn = New Data.DataColumn("CPRemarks")
        CPRemarks.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(CPRemarks)
        Dim CreateDate As Data.DataColumn = New Data.DataColumn("CreateDate")
        CreateDate.DataType = System.Type.GetType("System.DateTime")
        MyDT.Columns.Add(CreateDate)
        Dim CreateBy As Data.DataColumn = New Data.DataColumn("CreateBy")
        CreateBy.DataType = System.Type.GetType("System.String")
        MyDT.Columns.Add(CreateBy)
    End Sub

    Protected Sub btnBackEntry_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBackEntry.Click
        pnlEntry.Visible = True
        pnlPreview.Visible = False
    End Sub

    Protected Sub btnSearchREN_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSearchREN.Click
        GetStaff(repREN)
        mpeRENEntry.Show()
    End Sub

    Protected Sub repLister_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repLister.ItemCommand
        If e.CommandName = "SelectType" Then
            Dim Lister As String = CType(repLister.Items(e.CommandArgument).FindControl("lnkRepLister"), LinkButton).Text
            txtLister.Text = Lister
        End If
    End Sub

    Protected Sub btnSearchLister_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSearchLister.Click
        GetStaff(repLister)
        mpeListerEntry.Show()
    End Sub

    Protected Sub repREN_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repREN.ItemCommand
        If e.CommandName = "SelectType" Then
            Dim REN As String = CType(repREN.Items(e.CommandArgument).FindControl("lnkRepREN"), LinkButton).Text
            txtREN.Text = REN
        End If
    End Sub

    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
        If Session("Back") Is Nothing Then
            Response.Redirect("Documentation.aspx")
        Else
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")
            Response.Redirect(arr(0).ToString)
        End If
    End Sub

    Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click
        Dim TempDT As New Data.DataTable

        BtnSave_Click(sender, e)

        Dim CurrentUser As String = GetName()
        Dim PrintStr As String = "Printed by " & CurrentUser & " on " & Format(Now(), "dd-MMM-yyyy")

        TempDT = LoadDocExlusive()

        If TempDT IsNot Nothing Then
            Dim objRDLC As New RDLC("report/rptDocExclusive.rdlc", "DocExclusive")
            TempDT.TableName = "dsDocExclusive_AW_DocExclusive"
            objRDLC.AddParameter("PrintStr", PrintStr)
            objRDLC.AddDatabase(TempDT)
            objRDLC.ExportReport(RDLC.ExportTypeFormat.PDF, HttpContext.Current)
        End If
    End Sub
End Class
